package com.tile.v2;

import java.util.HashMap;
import java.util.Map;

public class DownLoadTile {
	private Map<String,String> maptypes = new HashMap<String, String>();
	private double minx;
	private double maxx;
	private double miny;
	private double maxy;
	private Integer minzoom;
	private Integer maxzoom;
	private String despath;
	private String servicepath;
	private int threadnum;
	

	public int getThreadnum() {
		return threadnum;
	}

	public void setThreadnum(int threadnum) {
		this.threadnum = threadnum;
	}

	public DownLoadTile(){
		maptypes.put("1", "http://mt1.google.cn/vt/lyrs=m@179000000&hl=zh-CN&gl=cn&src=app");	//地图
		maptypes.put("2", "http://mt3.google.cn/vt/lyrs=s@113&hl=zh-CN&gl=cn&src=app");		//卫星
	}
	
	public void init(double minx, double maxx, double miny, double maxy, Integer minzoom, Integer maxzoom){
		this.maxx = maxx;
		this.maxy = maxy;
		this.maxzoom = maxzoom;
		this.minx = minx;
		this.miny = miny;
		this.minzoom = minzoom;
	}
	
	public void init(String type, String boundstr, Integer minzoom, Integer maxzoom, String des){
		servicepath = maptypes.get(type);
		despath = des;
		String[] bounds = boundstr.split(",");
		double minx = Double.valueOf(bounds[0]);
		double miny = Double.valueOf(bounds[1]);
		double maxx = Double.valueOf(bounds[2]);
		double maxy = Double.valueOf(bounds[3]);
		
		init(minx, maxx, miny, maxy, minzoom, maxzoom);
	}

	public LevelTile getLevelTile(int zoom){
		Tile mintile = GetTileCoord.getTile(minx, maxy, zoom);
		Tile maxtile = GetTileCoord.getTile(maxx, miny, zoom);
		
		LevelTile lt = new LevelTile();
		lt.setMaxx(maxtile.getX());
		lt.setMaxy(maxtile.getY());
		lt.setMinx(mintile.getX());
		lt.setMiny(mintile.getY());
		
		System.out.println(zoom+"层"+(maxtile.getY() - mintile.getY())+"个文件夹");
		System.out.println("总共: "+(maxtile.getX() - mintile.getX()) * (maxtile.getY() - mintile.getY()));
		lt.setZoom(zoom);
		lt.setDespath(despath);
		lt.setServicepath(servicepath);
		lt.setTotaltn(threadnum);
		
		return lt;
	}
	
	public void download(){
		for(int i = minzoom; i<= maxzoom; i++){
			LevelTile lt = getLevelTile(i);
			int pre =(lt.maxy - lt.miny) / this.threadnum;
			int last = (lt.maxy - lt.miny) % this.threadnum;
			int more = 0;
			if(pre != 0){
				more = last/pre+1;
			}
			System.out.println(this.threadnum+more);
			for(int j =0; j< this.threadnum+more; j++){
				LevelTile nlt = getLevelTile(i);
				nlt.setTn(j);
				Thread t = new Thread(nlt);
				t.start();
			}
		}
	}
	
	
	public static void main(String[] args) {
		String type = PropertyManager.getProperty("maptype");
		String bounds = PropertyManager.getProperty("bounds");
		String minz = PropertyManager.getProperty("zoom");
		String maxz = minz;
		String des = PropertyManager.getProperty("savepath");
		
		Integer minzoom = Integer.valueOf(minz);
		Integer maxzoom = Integer.valueOf(maxz);
		
		DownLoadTile dlt = new DownLoadTile();
		dlt.init(type, bounds, minzoom, maxzoom, des);
		String num = PropertyManager.getProperty("threadnum");
		dlt.setThreadnum(Integer.valueOf(num));
		dlt.download();
	}
}
